package club.justwill.single.persistence.dao;

import club.justwill.single.persistence.jpo.ProjectIntroduce;
import club.justwill.single.persistence.support.BaseDao;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.Query;
import java.util.List;

/**
 * Created by luoyu on 15-12-27.
 */
@Repository
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public class ProjectIntroduceDao extends BaseDao<ProjectIntroduce> {

    public List<ProjectIntroduce> searchProjectIntroduce(Long projectId, int index, int size) {
        Query query = em.createNamedQuery("ProjectIntroduce.searchByProjectId")
                .setParameter("projectId",projectId)
                .setFirstResult(index)
                .setMaxResults(size);
        return (List<ProjectIntroduce>) query.getResultList();
    }


    public List<ProjectIntroduce> searchProjectSupportReturn(long projectId) {
        Query query = em.createNamedQuery("ProjectIntroduce,searchProjectSupportReturn")
                .setParameter("projectId",projectId);
        return query.getResultList();
    }

    public ProjectIntroduce searchByIntroduceId(Long introduceId) {
        Query query = em.createNamedQuery("ProjectIntroduce.searchByIntroduceId")
                .setParameter("introduceId",introduceId);
        try {
            return (ProjectIntroduce) query.getSingleResult();
        } catch (NonUniqueResultException e) {
            return (ProjectIntroduce)query.getResultList().get(0);
        } catch (NoResultException e) {
            return null;
        }
    }
}
